package com.colorsstudio.defence.Levels 
{
	import com.colorsstudio.defence.flashExportClass.*;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	
	import idv.cjcat.fenofsm.events.StateEvent;
	import idv.cjcat.fenofsm.Transition;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	import com.colorsstudio.defence.ConstantValues.Constants;
	import com.colorsstudio.defence.flashExportClass.f_achiev_desc_crazy;
	import com.colorsstudio.defence.flashExportClass.f_btnBack;
	
	/**
	 * ...
	 * @author Kababar
	 */
	public class AchievementsScene extends Level
	{
		//状态转换
		public var t_startScreen_achievementsScene:Transition;
		public var t_achievementsScene_startScreen:Transition;

		//从flash中载入的对象以及记录这些对象的数组
		public var tmpIcon:MovieClip;
		public var curSelectIcon:MovieClip = null;	//当前鼠标选中的成就图标
		public var clipCurAchievementDesc:MovieClip;//当前显示的成就描述
		public var btnBack:SimpleButton;// = new f_btnBack();
		public var spriteIconSelectFrame:Sprite = new f_achiev_icon_select_frame();
		public var achievementIcons:Array = new Array();
		
		//一些常量的定义
		public var StageUsableHeight:Number;
		public const AchievementsCountTotal:Number = 8;
		public const AchievementsCountPerLine:Number = 4;
		public const HorizontalSpace:Number = 8;
		public const VerticalSpace:Number = 8;
		
		//测试用变量
		public var testCounter:Number = 0;
		
		
		public function AchievementsScene() 
		{
			super();
		}
		
		//设置状态转换
		override public function setState():void
		{
			state = Constants.fsm.createState("AchievementsScene");
			t_startScreen_achievementsScene = Constants.fsm.createTransition(Constants.fsm.getStateByName("startScreen"), state, "toAchievementsScene", "t_startScreen_achievementsScene", 0);
			t_achievementsScene_startScreen = Constants.fsm.createTransition(state, Constants.fsm.getStateByName("startScreen"), "achievementsBackTostartScreen", "t_achievementsScene_startScreen", 0);
			super.setState();
		}
		
		//场景开始
		override public function levelStart(e:StateEvent):void 
		{
			super.levelStart(e);
			trace("AchievementsScene Start");
			Constants.main.addChild(this);
			StageUsableHeight = stage.stageHeight * .80;
			thisLevelGosHere();
		}
		
		//"场景开始"的细分步骤
		public function thisLevelGosHere():void 
		{
			//显示返回按钮
			btnBack = new f_btnBack();
			this.layer2.addChild(btnBack);
			btnBack.addEventListener(MouseEvent.CLICK, clickBtnBack);
			
			
			//创建并显示所有成就的图标在底部(如果该成就已经解锁,显示成就对应的图标,否则为一个问号)
			createAchievementIcons();
			displayAchievementIcons();
			
			/*显示当前选中的成就的描述*/
			clipCurAchievementDesc = new f_achiev_desc_locked();
			this.layer3.addChild(clipCurAchievementDesc);
			displayAchievementDescription();
			
		}
		
		/*创建表示成就的小图标
		* 如果成就已经解锁,就会压入成就对应的图标
		* 否则压入一个锁定的标志
		* */
		public function createAchievementIcons():void
		{
			//设置解锁,只需运行一次.
			Constants.gameData.data.achievement_crazy = false;
			Constants.gameData.data.achievement_god = true;
			Constants.gameData.data.achievement_hundred = false;
			Constants.gameData.data.achievement_master = true;
			Constants.gameData.data.achievement_ready = false;
			Constants.gameData.data.achievement_recruit = false;
			Constants.gameData.data.achievement_rigid = true;
			Constants.gameData.data.achievement_survival = true;
			
			//成就"Crazy"
			if (Constants.gameData.data.achievement_crazy)
			{
				achievementIcons.push( new f_achiev_icon_crazy() );
			}
			else
			{
				achievementIcons.push( new f_achiev_icon_locked() );
			}
			
			//成就"God"
			if (Constants.gameData.data.achievement_god)
			{
				achievementIcons.push( new f_achiev_icon_god() );
			}
			else
			{
				achievementIcons.push( new f_achiev_icon_locked() );
			}
			
			//成就"Hundred"
			if (Constants.gameData.data.achievement_hundred)
			{
				achievementIcons.push( new f_achiev_icon_hundred() );
			}
			else
			{
				achievementIcons.push( new f_achiev_icon_locked() );
			}
			
			//成就"Master"
			if (Constants.gameData.data.achievement_master)
			{
				achievementIcons.push( new f_achiev_icon_master() );
			}
			else
			{
				achievementIcons.push( new f_achiev_icon_locked() );
			}
			
			//成就"Ready"
			if (Constants.gameData.data.achievement_ready)
			{
				achievementIcons.push( new f_achiev_icon_ready() );
			}
			else
			{
				achievementIcons.push( new f_achiev_icon_locked() );
			}
			
			//成就"Recruit"
			if (Constants.gameData.data.achievement_recruit)
			{
				achievementIcons.push( new f_achiev_icon_recruit() );
			}
			else
			{
				achievementIcons.push( new f_achiev_icon_locked() );
			}
			
			//成就"Regid"
			if (Constants.gameData.data.achievement_rigid)
			{
				achievementIcons.push( new f_achiev_icon_rigid() )
			}
			else
			{
				achievementIcons.push( new f_achiev_icon_locked() );
			}
			
			//成就"Survival"
			if (Constants.gameData.data.achievement_survival)
			{
				achievementIcons.push( new f_achiev_icon_survival() );
			}
			else
			{
				achievementIcons.push( new f_achiev_icon_locked() );
			}
		}
		
		//把成就图标显示出来
		public function displayAchievementIcons():void
		{
			curSelectIcon = tmpIcon = achievementIcons[0];
			var lines:Number = Math.ceil(AchievementsCountTotal / AchievementsCountPerLine);
			var x:Number = (stage.stageWidth - AchievementsCountPerLine * (tmpIcon.width + HorizontalSpace) + HorizontalSpace) / 2 + tmpIcon.width / 2;
			var y:Number = StageUsableHeight - lines * (tmpIcon.height + VerticalSpace) + tmpIcon.height / 2;
			trace("lines = " + lines + ", x = " + x + ", y = " + y);
			for (var curline:int = 0, counter:int = 0; counter+curline*AchievementsCountPerLine < AchievementsCountTotal; counter++)
			{
				if (counter >= AchievementsCountPerLine)
				{
					counter -= AchievementsCountPerLine;
					curline++;
				}
				tmpIcon = achievementIcons[counter + curline * AchievementsCountPerLine];
				tmpIcon.x = x + counter * (tmpIcon.width + HorizontalSpace);
				tmpIcon.y = y + curline * (tmpIcon.height + VerticalSpace);
				this.layer2.addChild(achievementIcons[counter + curline * AchievementsCountPerLine]);
				tmpIcon.scaleX = 0;
				tmpIcon.scaleY = 0;
				TweenLite.to(tmpIcon, 1, { x: tmpIcon.x, y: tmpIcon.y, scaleX: 1, scaleY: 1, ease: Elastic.easeOut } );
				tmpIcon.addEventListener(MouseEvent.MOUSE_OVER, mouseOverIconHandler);
			}
			//trace("curSelectIcon = " + achievementIcons.indexOf(curSelectIcon));
		}
		
		/* 显示成就描述
		 * 如果改成就已经被解锁,则显示详细描述
		 * 否则显示"未知"信息
		 */
		public function displayAchievementDescription():void
		{
			this.layer3.removeChild(clipCurAchievementDesc);			
			
			switch(achievementIcons.indexOf(curSelectIcon))
			{
				//未选中任何一个图标
				case -1:
					clipCurAchievementDesc = new f_achiev_desc_locked();
					break;
					
				//选中成就"Crazy"	
				case 0:	
					if (Constants.gameData.data.achievement_crazy)
					{
						clipCurAchievementDesc = new f_achiev_desc_crazy();
					}
					else
					{
						clipCurAchievementDesc = new f_achiev_desc_locked();
					}
					break;
					
				//选中成就"God"	
				case 1:	
					if (Constants.gameData.data.achievement_god)
					{
						clipCurAchievementDesc = new f_achiev_desc_god();
					}
					else
					{
						clipCurAchievementDesc = new f_achiev_desc_locked();
					}
					
					break;
					
				//选中成就"Hundred"	
				case 2:		
					if (Constants.gameData.data.achievement_hundred)
					{
						clipCurAchievementDesc = new f_achiev_desc_hundred();
					}
					else
					{
						clipCurAchievementDesc = new f_achiev_desc_locked();
					}
					break;
					
				//选中成就"Master"	
				case 3:		
					if (Constants.gameData.data.achievement_master)
					{
						clipCurAchievementDesc = new f_achiev_desc_master();
					}
					else
					{
						clipCurAchievementDesc = new f_achiev_desc_locked();
					}
					break;
					
				//选中成就"Ready"	
				case 4:		
					if (Constants.gameData.data.achievement_ready)
					{
						clipCurAchievementDesc = new f_achiev_desc_ready();
					}
					else
					{
						clipCurAchievementDesc = new f_achiev_desc_locked();
					}
					break;
					
					
				//选中成就"Recruit"	
				case 5:		
					if (Constants.gameData.data.achievement_recruit)
					{
						clipCurAchievementDesc = new f_achiev_desc_recruit();
					}
					else
					{
						clipCurAchievementDesc = new f_achiev_desc_locked();
					}
					break;
					
				//选中成就"Rigid"						
				case 6:		
					if (Constants.gameData.data.achievement_rigid)
					{
						clipCurAchievementDesc = new f_achiev_desc_rigid();
					}
					else
					{
						clipCurAchievementDesc = new f_achiev_desc_locked();
					}
					break;
					
				//选中成就"Survival"	
				case 7:		
					if (Constants.gameData.data.achievement_survival)
					{
						clipCurAchievementDesc = new f_achiev_desc_survival();
					}
					else
					{
						clipCurAchievementDesc = new f_achiev_desc_locked();
					}
					break;
					
				default:
					clipCurAchievementDesc = new f_achiev_desc_locked();
					break;					
			}
			this.layer3.addChild(clipCurAchievementDesc);
		}
		
		//当鼠标在图标上方时,会有一个框框住它
		public function mouseOverIconHandler(e:MouseEvent)
		{
			(e.target as MovieClip).removeEventListener(MouseEvent.MOUSE_OVER, mouseOverIconHandler);
			trace("Over times: " + ++testCounter);
			spriteIconSelectFrame.x = e.target.x;
			spriteIconSelectFrame.y = e.target.y;
			this.layer1.addChild(spriteIconSelectFrame);
			curSelectIcon = e.target as MovieClip;
			displayAchievementDescription();
			//trace("curSelectIcon = " + achievementIcons.indexOf(curSelectIcon));
			(e.target as MovieClip).addEventListener(MouseEvent.MOUSE_OUT, mouseOutIconHandler);
		}
		
		//当鼠标从成就图标上移出时
		public function mouseOutIconHandler(e:MouseEvent)
		{
			(e.target as MovieClip).addEventListener(MouseEvent.MOUSE_OVER, mouseOverIconHandler);
			//this.layer1.removeChild(spriteIconSelectFrame);
		}
		
		//鼠标点中"Back"按钮
		public function clickBtnBack(e:Event):void
		{
			Constants.fsm.input("achievementsBackTostartScreen");
		}
		
		
		//场景结束
		override public function levelEnd(e:StateEvent):void 
		{
			super.levelEnd(e);
			Constants.main.removeChild(this);
		}
	}

}
